﻿using LinearRegression.models;

namespace LinearRegression
{
    internal class Program
    {
        /// <summary>
        /// 用最小二乘法估计中国2020年GDP总量
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            double[] x = new double[17];
            for (int i = 0; i < 17; i++)
            {
                x[i] = i + 2003;
            }
            double[] y = new double[17]
                {
                1660287965663,
                1955347004965,
                2285965892364,
                2752131773358,
                3550342737009,
                4594307032667,
                5101703073088,
                6087163874510,
                7551500124197,
                8532229986993,
                9570406235659,
                10475682920597,
                11061553079871,
                11233276536744,
                12310409370894,
                13894817549380,
                14279937500608,    
                };
            double avex = x.Average();
            double avey = y.Average();
            double gdpInReality = 14687673892882;
            double b = LeastSquare.Getb(x, y, avex, avey);
            double a = LeastSquare.Geta(avex, avey, b);
            double year = 2020;
            double gdpInSuppose = b * year + a;
            double deviation = (gdpInSuppose - gdpInReality) / gdpInReality * 100;
            Console.WriteLine($"预测{year}年中国GDP：{gdpInSuppose}");
            Console.WriteLine($"{year}年实际GDP：{gdpInReality}");
            Console.WriteLine($"线性回归模型的误差：{deviation}%");
            Console.ReadKey();
        }
    }
}